<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-28 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	function excute()
	{
		ob_start();

		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			case 'search':
				$this->search();
				break;
			case 'do_search':
				$this->doSearch();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			default:
				$this->main();
				break;
		}

		$body = ob_get_clean();

		$this->showBody($body);
	}
	
	private function main()
	{
		$this->doSearch();
	}

	private function add()
	{
		$request = array();
		if(isset($this->request['c']))
		{
			$request = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$request['username'] = isset($request['username']) ? trim($request['username']) : '';
		$request['group_id'] = isset($request['group_id']) ? (int)$request['group_id'] : '';
		$request['email'] = isset($request['email']) ? trim($request['email']) : '';
		
		$group_list = get_user_group_list();
		
		$groups = array();
		foreach($group_list as $k=>$v)
		{
			$groups[] = array('name'=>$v['name'], 'value'=>$v['id']);
		}
		
		require_once ROOT_PATH.'include/function/form.func.php';
		$select = select('group_id', $groups, $request['group_id'], 'style="width:100px;"');
		
		$action = "do_add";
		include template('admin/user_add');
	}
	
	private function doAdd()
	{
		$request['username'] = isset($this->request['username']) ? trim($this->request['username']) : '';
		$request['password'] = isset($this->request['password']) ? trim($this->request['password']) : '';
		$request['group_id'] = isset($this->request['group_id']) ? trim($this->request['group_id']) : '';
		$request['email'] = isset($this->request['email']) ? trim($this->request['email']) : '';
		
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "admin.php?m=user&a=add&c=$key";
		
		if(!$request['username'] || !$request['password'] || !$request['email'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		
		$request['time'] = time();
		$request['password'] = md5($request['time'].''.(md5($request['password'])));
		
		$group_list = get_user_group_list();
		foreach($group_list as $k=>$v)
		{
			if($v['id'] == $request['group_id'])
			{
				$request['is_admin'] = $v['is_admin'];
				break;
			}
		}
		$request['ip'] = client_ip();
		
		$result = add_user($request);
		
		if($result)
		{
			$this->messager("添加成功", 'admin.php?m=user&a=search');
		}
		else
		{
			$this->messager("添加失败", $redirect);
		}
	}
	
	private function doDelete()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		if(!$ids)
		{
			$this->messager("请指定ID", 'admin.php?m=review');
		}
		
		$result = delete_user($ids);
		
		if($result)
		{
			$this->messager("删除成功", 'admin.php?m=review');
		}
		else
		{
			$this->messager("删除失败", 'admin.php?m=review');
		}
	}
	
	private function search()
	{
		$group_list = get_user_group_list();

		$role_count = count($group_list) + 1;
		
		$action = "do_search";
		include template('admin/user_search');
	}
	
	private function doSearch()
	{
		$page = $this->page_num + 1;
				
		$group_id = isset($this->request['group_id']) ? trim($this->request['group_id']) : '';
		$username = isset($this->request['username']) ? trim($this->request['username']) : '';
		$nickname = isset($this->request['nickname']) ? trim($this->request['nickname']) : '';
		$email = isset($this->request['email']) ? trim($this->request['email']) : '';
		$regip = isset($this->request['regip']) ? trim($this->request['regip']) : '';
		$lastip = isset($this->request['lastip']) ? trim($this->request['lastip']) : '';
		
		if(check_array($group_id))
		{
			$group_id = implode(',', $group_id);
		}
		
		$query_link = "admin.php?m=user";
		$where_list = array();
				
		if($group_id)
		{
			$where_list['group'] = "u.group_id in ($group_id)";
			$query_link .= "&group_id=".urlencode($group_id);
		}
		
		if($username)
		{
			if(substr($username, 0, 1) == '*' || substr($username, -1) == '*')
			{
				$username = (substr($username, 0, 1) == '*') ? '%'.substr($username, 1) : $username;
				$username = (substr($username, -1) == '*') ? substr($username, 0, -1).'%' : $username;
				$where_list['username'] = " u.`name` like '$username'";
			}
			else
			{
				$where_list['username'] = " u.`name` = '$username'";
			}
			$query_link .= "&username=".urlencode($username);
		}
		
		if($nickname)
		{
			if(substr($nickname, 0, 1) == '*' || substr($nickname, -1) == '*')
			{
				$username = (substr($nickname, 0, 1) == '*') ? '%'.substr($nickname, 1) : $nickname;
				$nickname = (substr($nickname, -1) == '*') ? substr($nickname, 0, -1).'%' : $nickname;
				$where_list['nickname'] = " u.`nick_name` like '$nickname'";
			}
			else
			{
				$where_list['nickname'] = " u.`nick_name` = '$username'";
			}
			$query_link .= "&nickname=".urlencode($nickname);
		}
		
		if($email)
		{
			if(substr($email, 0, 1) == '*' || substr($email, -1) == '*')
			{
				$email = (substr($email, 0, 1) == '*') ? '%'.substr($email, 1) : $email;
				$email = (substr($email, -1) == '*') ? substr($email, 0, -1).'%' : $email;
				$where_list['email'] = "u.`email` like '$email'";
			}
			else
			{
				$where_list['email'] = "u.`email` = '$email'";
			}
			$query_link .= "&email=".urlencode(email);
		}
		
		if($regip)
		{
			$where_list['regip'] = "u.`register_ip` like '$regip%'";
			$query_link .= "&regip=".urlencode($regip);
		}
		
		if($regip)
		{
			$where_list['lastip'] = "u.`last_ip` like '$lastip%'";
			$query_link .= "&lastip=".urlencode($lastip);
		}
		
		$where = empty($where_list) ? '' : ' where '.implode(' and ', $where_list).' ';
		
		$sql = " select count(1) as `total_record` from `".TABLE_PREFIX."user` u {$where} ";
		$total_record = DBFunction::getInstance()->getOne($sql);
		
		$item_list = array();
		if($total_record > 0)
		{
			$offset = $this->page_num * $this->page_limit;
			$sql = "select u.*, g.`name` as group_name 
						from `".TABLE_PREFIX."user` u 
						inner join `".TABLE_PREFIX."user_group` g on g.`id` = u.`group_id`
						{$where} 
						order by u.`id` asc 
						limit {$offset},{$this->page_limit} ";
			$item_list = DBFunction::getInstance()->getAll($sql);
		}
		
		$page_html = page($total_record, $this->page_limit, $page, $query_link, '20 50 100 200 500');
		
		$action = "do_delete";
		include template('admin/user_search_list');
	}
	
	private function modify()
	{
		$user_id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$user_id)
		{
			$this->messager("请指定ID", 'admin.php?m=user');
		}
		
		if(isset($this->request['c']) && ($request = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{
			$request = get_user($user_id);
			
			if(!isset($request['id']) || !$request['id'])
			{
				$this->messager("用户不存在", 'admin.php?m=user');
			}
		}
		
		$group_list = get_user_group_list();
		
		$groups = array();
		foreach($group_list as $k=>$v)
		{
			$groups[] = array('name'=>$v['name'], 'value'=>$v['id']);
		}
		
		require_once ROOT_PATH.'include/function/form.func.php';
		$select = select('group_id', $groups, $request['group_id'], 'style="width:100px;"');
		
		$action = 'do_modify&id='.$request['id'];
		$action_name = '编辑用户';
		include template('admin/user_info');
	}
	
	private function doModify()
	{
		
	}
}